//
//  WXCallBack.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/12/11.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import soapp
import Kingfisher

public class WXShare:NSObject,MoShareRealization{
    static var _default:WXShare? = nil
    public static var `default`:WXShare{
        get{
            guard let obj =  _default else{
                let obj = WXShare()
                _default = obj
                return obj
            }
            return obj
        }
    }
    
    public func share(media: MoShare.Media, sence: MoShare.Scene, callBack: ShareCallBack?) {
        switch media {
               //                case .image(title: let title, desc: let desc, thumb: let thumb, image: let img):
               //                    return SendMessageToWXReq()
               //                case .video(title: let title, desc: let desc, thumb: let thumb):
               //                    break
               //                case .music(title: let title, desc: let desc, thumb: let thumb, url: let url):
               //                    break
        case .wxProgram(title: let title, desc: let desc, thumb: let thumb, program: let program):
            let msg = WXMediaMessage()
            let obj = program
            
            switch AppEnvironment.current {
            case .pro:
                program.miniProgramType = .release
            default:
                program.miniProgramType = .preview
            }
            
            msg.title = title
            msg.description = desc
            msg.thumbData = nil
            msg.mediaObject = obj

            if let img = thumb as? UIImage{
                 msg.setThumbImage(img)
//                obj.hdImageData = img.jpegData(compressionQuality: 0.4)
//                    msg.thumbData = img.jpegData(compressionQuality: 0.7)
            }else if let url = thumb as? String{
             UIImage.kf_cache(url: url, finish: { (img) in
                 if let img_ = img{
                     msg.setThumbImage(img_)
//                        obj.hdImageData = img_.jpegData(compressionQuality: 0.4)
                 }
             })
            }else{}
            
            let req = SendMessageToWXReq()
            req.scene = Int32(WXSceneSession.rawValue)
            req.bText = false
            req.message = msg
            
            WXApi.send(req) { (success) in
                print("P)))))---LLLLL",success)
            }
        case .webpage(title: let title, desc: let desc, thumb: let thumb, url: let url):

               let msg = WXMediaMessage()
               let obj = WXWebpageObject()
               obj.webpageUrl = url
               msg.title = title
               msg.description = desc
               if let img = thumb as? UIImage{
                    msg.setThumbImage(img)
//                    msg.thumbData = img.jpegData(compressionQuality: 0.7)
               }else if let url = thumb as? String{
                UIImage.kf_cache(url: url, finish: { (img) in
                    if let img_ = img{
                        msg.setThumbImage(img_)
                    }
                    
                })
//                    if let img = UIImage.kf_cache(url:url){
//                        msg.setThumbImage(img)
////                        msg.thumbData = img.jpegData(compressionQuality: 0.7)
//                    }
               }else{}
               msg.mediaObject = obj
               let req = SendMessageToWXReq()
//               req.bText = true
//               req.text = "分享的内容";
//               req.scene = WXSceneSession;
               req.scene = Int32(sence.rawValue)
               req.message = msg
               print("opaososso---",req.scene)
               WXApi.send(req) { (success) in
                print("++++OSOOSOS\(success)")
               }
           default:
               break
           }
    }
    
    public var plat: String = "WX"
    
    
}

